package org.jeecg.modules.demo.navigation.service.impl;

import org.apache.xmlbeans.impl.regex.Match;
import org.jeecg.modules.demo.navigation.entity.Navigation;
import org.jeecg.modules.demo.navigation.entity.Information;
import org.jeecg.modules.demo.navigation.mapper.InformationMapper;
import org.jeecg.modules.demo.navigation.mapper.NavigationMapper;
import org.jeecg.modules.demo.navigation.service.INavigationService;
import org.springframework.stereotype.Service;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.transaction.annotation.Transactional;
import java.io.Serializable;
import java.util.List;
import java.util.Collection;
import java.util.regex.Matcher;
import java.util.regex.Pattern;

/**
 * @Description: 导航栏
 * @Author: jeecg-boot
 * @Date:   2019-09-18
 * @Version: V1.0
 */
@Service
public class NavigationServiceImpl extends ServiceImpl<NavigationMapper, Navigation> implements INavigationService {

	@Autowired
	private NavigationMapper navigationMapper;
	@Autowired
	private InformationMapper informationMapper;

	@Override
	public List<Navigation> queryListCode(String typeStr) {
		List<Navigation> list=null;
		String code=null;
		try{
			code=navigationMapper.queryCodeByName(typeStr);
			if (code.equals("")||code==null){
				throw new Exception("无此类型编码");
			}
			list=navigationMapper.queryListCode(code+"C%");
		}catch (Exception e){
			e.printStackTrace();
		}
		return list;
	}

	@Override
	@Transactional
	public void saveMain(Navigation navigation, List<Information> informationList) {
		navigationMapper.insert(navigation);
		if(informationList!=null && informationList.size()>0) {
			for(Information entity:informationList) {
				//外键设置
				entity.setTypeCode(navigation.getCode());
				informationMapper.insert(entity);
			}
		}
	}

	@Override
	@Transactional
	public void updateMain(Navigation navigation,List<Information> informationList) {
		navigationMapper.updateById(navigation);
		
		//1.先删除子表数据
		informationMapper.deleteByMainId(navigation.getId());
		
		//2.子表数据重新插入
		if(informationList!=null && informationList.size()>0) {
			for(Information entity:informationList) {
				//外键设置
				entity.setTypeCode(navigation.getCode());
				informationMapper.insert(entity);
			}
		}
	}

	@Override
	@Transactional
	public void delMain(String id) {
		informationMapper.deleteByMainId(id);
		navigationMapper.deleteById(id);
	}

	@Override
	@Transactional
	public void delBatchMain(Collection<? extends Serializable> idList) {
		for(Serializable id:idList) {
			informationMapper.deleteByMainId(id.toString());
			navigationMapper.deleteById(id);
		}
	}
	
}
